package com.lqk.buffer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class BufferDemo2 {

    // 内存映射文件IO
    static private final int start=0;
    static private final int size=1024;
    private static void b04() throws Exception {
        RandomAccessFile raf = new RandomAccessFile("f:\\lqk\\01.txt", "rw");
        FileChannel fileChannel = raf.getChannel();
        MappedByteBuffer mbb=fileChannel.map(FileChannel.MapMode.READ_WRITE,start,size);

        mbb.put(0,(byte) 97);
        mbb.put(1023,(byte) 122);

        raf.close();
    }

    // 直接缓冲区
    private static void b03() throws Exception {
        String inFile="f:\\lqk\\01.txt";
        FileInputStream fis=new FileInputStream(inFile);    // 文件输入流
        FileChannel fisChannel = fis.getChannel();

        String outFile="f:\\lqk\\02.txt";
        FileOutputStream fos=new FileOutputStream(outFile);
        FileChannel fosChannel = fos.getChannel();

        // 创建直接缓冲区
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

        while (true){
            buffer.clear();

            int read = fisChannel.read(buffer);
            if (read == -1){
                break;
            }

            buffer.flip();
            fosChannel.write(buffer);
        }
        fis.close();
        fisChannel.close();
        fos.close();
        fosChannel.close();
    }

    // 只读缓冲区
    private static void b02(){
        // 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(10);
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }
        // 创建只读缓冲区
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();

        for (int i = 0; i < buffer.capacity(); i++) {
            byte b = buffer.get(i);
            b*=10;
            buffer.put(i,b);
        }

        readOnlyBuffer.position(0);
        readOnlyBuffer.limit(buffer.capacity());

        while (readOnlyBuffer.hasRemaining()){
            System.out.println(readOnlyBuffer.get());
        }
    }

    // 缓冲区分片
    private static void b01(){
        // 创建 Buffer
        ByteBuffer buffer = ByteBuffer.allocate(10);
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }
        //创建子缓冲区
        buffer.position(3); //设置buffer的position位置
        buffer.limit(7);    //设置buffer的limit位置
        ByteBuffer slice = buffer.slice();//创建子缓冲区

        //改变子缓冲区内容
        for (int i = 0; i < slice.capacity(); i++) {
            byte b = slice.get(i);
            b *= 10;
            slice.put(i,b);
        }

        buffer.position(0);
        buffer.limit(buffer.capacity());

        while (buffer.remaining()>0){
            System.out.println(buffer.get());
        }

    }
    public static void main(String[] args) throws Exception {
//        BufferDemo2.b01();
//        BufferDemo2.b02();
        BufferDemo2.b03();
    }
}
